home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / MacPNG Library 1.02 / pngMacSrc 1.02 / png.1 / ptot / ptot.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-29  |  11.2 KB  |  379 lines  |  [TEXT/CWIE]

  1. /*
  2.  * ptot.h
  3.  *
  4.  * Header file for PNG to TIFF converter.
  5.  *
  6.  **********
  7.  *
  8.  * HISTORY
  9.  * 95-08-29 Update to compile on the Macintosh computer 
  10.  *          by Mark Fleming (RMF) <markf@post.queensu.ca>
  11.  * 95-03-10 Created by Lee Daniel Crocker <lee@piclab.com>
  12.  *          <URL:http://www.piclab.com/piclab/index.html>
  13.  */
  14. #if defined(applec) || defined(__MWERKS__) || defined(THINK_C)        /* RMF Added */
  15. #define MACOS
  16. #define STDC
  17. #  define BIG_ENDIAN
  18. #  ifndef FILENAME_MAX /* Work around stupid header file bugs */
  19. #    define FILENAME_MAX 32
  20. #  endif
  21. #  ifndef SEEK_SET
  22. #    define SEEK_SET 0
  23. #  endif
  24. #  ifndef min
  25. #    define min(x,y) (((x)<(y))?(x):(y))
  26. #  endif
  27. #  ifndef max
  28. #    define max(x,y) (((y)<(x))?(x):(y))
  29. #  endif
  30. #endif
  31.  
  32. #ifdef _X86_            /* Intel i86 family */
  33. #  define LITTLE_ENDIAN
  34. #endif
  35.  
  36. #ifdef _SPARC_
  37. #  define BIG_ENDIAN
  38. #  ifndef FILENAME_MAX /* Work around stupid header file bugs */
  39. #    define FILENAME_MAX 1024
  40. #  endif
  41. #  ifndef SEEK_SET
  42. #    define SEEK_SET 0
  43. #  endif
  44. #  ifndef min
  45. #    define min(x,y) (((x)<(y))?(x):(y))
  46. #  endif
  47. #  ifndef max
  48. #    define max(x,y) (((y)<(x))?(x):(y))
  49. #  endif
  50. #endif
  51.  
  52. /*
  53.  * Some types and macros for easier porting. Byte swapping is the
  54.  * major issue because we have to convert big-endiang PNG to
  55.  * native-endian TIFF on whatever architecture we're compiled on.
  56.  * The code depends heavily on the endianness definition above.
  57.  * Functions would be a lot simpler than macros here, but are less
  58.  * likely to be optimized down to simple inline byte swaps. Note
  59.  * that some of these macros evaluate the address twice, so don't
  60.  * pass "*p++" to them!
  61.  */
  62.  
  63. typedef signed char     S8;
  64. typedef unsigned char   U8;
  65. typedef signed short    S16;
  66. typedef unsigned short  U16;
  67. typedef signed long     S32;
  68. typedef unsigned long   U32;
  69.  
  70. #ifndef TRUE
  71. #  define TRUE 1
  72. #  define FALSE 0
  73. #endif
  74.  
  75. #define LOBYTE(w)   ((U8)((w)&0xFF))
  76. #define HIBYTE(w)   ((U8)(((w)>>8)&0xFF))
  77. #define LOWORD(d)   ((U16)((d)&0xFFFF))
  78. #define HIWORD(d)   ((U16)(((d)>>16)&0xFFFF))
  79.  
  80. #define PUT16(p,w)  (*(U16*)(p)=(w))    /* Native byte order */
  81. #define GET16(p)    (*(U16*)(p))
  82. #define PUT32(p,d)  (*(U32*)(p)=(d))
  83. #define GET32(p)    (*(U32*)(p))
  84.  
  85. #if !defined(BIG_ENDIAN) && !defined(LITTLE_ENDIAN)
  86. #  error "No byte order defined"
  87. #endif
  88.  
  89. #ifdef BIG_ENDIAN
  90. #  define BE_GET16(p)     GET16(p)
  91. #  define BE_PUT16(p,w)   PUT16((p),(w))
  92. #  define BE_GET32(p)     GET32(p)
  93. #  define BE_PUT32(p,d)   PUT32((p),(d))
  94. #  define LE_GET16(p)     ((U16)(*(U8*)(p)&0xFF)|\
  95.                             (*((U8*)(p)+1)<<8))
  96. #  define LE_PUT16(p,w)   (((*(U8*)(p))=LOBYTE(w)),\
  97.                             ((*((U8*)(p)+1))=HIBYTE(w)))
  98. #  define LE_GET32(p)     (((U32)LE_GET16(p))|\
  99.                             LE_GET16((U8*)(p)+2)<<16)
  100. #  define LE_PUT32(p,d)   (LE_PUT16((p),LOWORD(d)),\
  101.                             LE_PUT16((U8*)(p)+2,HIWORD(d)))
  102. #else
  103. #  define BE_GET16(p)     ((U16)(*(U8*)(p)<<8)|\
  104.                             (*((U8*)(p)+1)&0xFF))
  105. #  define BE_PUT16(p,w)   (((*(U8*)(p))=HIBYTE(w)),\
  106.                             ((*((U8*)(p)+1))=LOBYTE(w)))
  107. #  define BE_GET32(p)     (((U32)BE_GET16(p)<<16)|\
  108.                             BE_GET16((U8*)(p)+2))
  109. #  define BE_PUT32(p,d)   (BE_PUT16((p),HIWORD(d)),\
  110.                             BE_PUT16((U8*)(p)+2,LOWORD(d)))
  111. #  define LE_GET16(p)     GET16(p)
  112. #  define LE_PUT16(p,w)   PUT16((p),(w))
  113. #  define LE_GET32(p)     GET32(p)
  114. #  define LE_PUT32(p,d)   PUT32((p),(d))
  115. #endif
  116.  
  117. /*
  118.  * Miscellaneous PNG definitions.
  119.  */
  120.  
  121. #define PNG_Signature       "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A"
  122. #define PNG_MaxChunkLength  0x7FFFFFFFL
  123.  
  124. #define PNG_CN_IHDR 0x49484452L     /* Chunk names */
  125. #define PNG_CN_PLTE 0x504C5445L
  126. #define PNG_CN_IDAT 0x49444154L
  127. #define PNG_CN_IEND 0x49454E44L
  128. #define PNG_CN_gAMA 0x67414D41L
  129. #define PNG_CN_sBIT 0x73424954L
  130. #define PNG_CN_cHRM 0x6348524DL
  131. #define PNG_CN_tRNS 0x74524E53L
  132. #define PNG_CN_bKGD 0x624B4744L
  133. #define PNG_CN_hIST 0x68495354L
  134. #define PNG_CN_tEXt 0x74455874L
  135. #define PNG_CN_zTXt 0x7A545874L
  136. #define PNG_CN_pHYs 0x70485973L
  137. #define PNG_CN_oFFs 0x6F464673L
  138. #define PNG_CN_tIME 0x74494D45L
  139. #define PNG_CN_sCAL 0x7343414CL
  140.  
  141. #define PNG_CF_Ancillary    0x20000000L /* Chunk flags */
  142. #define PNG_CF_Private      0x00200000L
  143. #define PNG_CF_CopySafe     0x00000020L
  144.  
  145. #define PNG_FT_Adaptive 0   /* Filtering type */
  146. #define PNG_CT_Deflate  0   /* Compression type */
  147. #define PNG_IT_None     0   /* Interlace types */
  148. #define PNG_IT_Costello 1
  149.  
  150. #define PNG_CB_Palette  0x01    /* Colortype bits */
  151. #define PNG_CB_Color    0x02
  152. #define PNG_CB_Alpha    0x04
  153.  
  154. #define PNG_MU_None         0   /* Measurement units */
  155. #define PNG_MU_Pixel        0
  156. #define PNG_MU_Meter        1
  157. #define PNG_MU_Micrometer   1
  158. #define PNG_MU_Radian       2
  159.  
  160. #define PNG_PF_None     0   /* Prediction filters */
  161. #define PNG_PF_Sub      1
  162. #define PNG_PF_Up       2
  163. #define PNG_PF_Average  3
  164. #define PNG_PF_Paeth    4
  165.  
  166. /*
  167.  * Miscellaneous TIFF definitions. This is a small subset of
  168.  * the tags, data types, and values available in TIFF--only
  169.  * the ones needed for PNG conversion.  For example, we are
  170.  * not doing any TIFF compression, so the only TIFF compression
  171.  * type listed is "None".
  172.  */
  173.  
  174. #define TIFF_BO_Intel       0x4949  /* Byte order identifiers */
  175. #define TIFF_BO_Motorola    0x4D4D
  176. #define TIFF_MagicNumber    42
  177.  
  178. #define TIFF_DT_BYTE        1   /* Data types */
  179. #define TIFF_DT_ASCII       2
  180. #define TIFF_DT_SHORT       3
  181. #define TIFF_DT_LONG        4
  182. #define TIFF_DT_RATIONAL    5
  183. #define TIFF_DT_UNDEFINED   7
  184.  
  185. #define TIFF_TAG_ImageWidth         256 /* Tag values */
  186. #define TIFF_TAG_ImageLength        257
  187. #define TIFF_TAG_BitsPerSample      258
  188. #define TIFF_TAG_Compression        259
  189. #define TIFF_TAG_PhotometricInterpretation  262
  190. #define TIFF_TAG_ImageDescription   270
  191. #define TIFF_TAG_Make               271
  192. #define TIFF_TAG_Model              272
  193. #define TIFF_TAG_StripOffsets       273
  194. #define TIFF_TAG_SamplesPerPixel    277
  195. #define TIFF_TAG_RowsPerStrip       278
  196. #define TIFF_TAG_StripByteCounts    279
  197. #define TIFF_TAG_XResolution        282
  198. #define TIFF_TAG_YResolution        283
  199. #define TIFF_TAG_PlanarConfiguration        284
  200. #define TIFF_TAG_XPosition          286
  201. #define TIFF_TAG_YPosition          287
  202. #define TIFF_TAG_ResolutionUnit     296
  203. #define TIFF_TAG_TransferFunction   301
  204. #define TIFF_TAG_Software           305
  205. #define TIFF_TAG_DateTime           306
  206. #define TIFF_TAG_Artist             315
  207. #define TIFF_TAG_HostComputer       316
  208. #define TIFF_TAG_WhitePoint         318
  209. #define TIFF_TAG_PrimaryChromaticities      319
  210. #define TIFF_TAG_ColorMap           320
  211. #define TIFF_TAG_ExtraSamples       338
  212. #define TIFF_TAG_Copyright          33432
  213.  
  214. /*
  215.  * This last tag is registered to me specifically for this
  216.  * program and its companion TIFF-to-PNG (not included in
  217.  * the DDJ code), so that they can be invertable. I encourage
  218.  * others to use it for the same purpose--just make sure you
  219.  * stay compatible with this program.
  220.  *
  221.  * There is no equivalent PNG chunk for TIFF data; the known
  222.  * TIFF tags can be either translated to functionally
  223.  * equivalent PNG chunks or encoded in tEXt chunks. Unknown
  224.  * ones are not copy-safe (according to the TIFF spec).
  225.  */
  226.  
  227. #define TIFF_TAG_PNGChunks          34865
  228.  
  229. #define TIFF_CT_NONE    1   /* Compression type */
  230. #define TIFF_PI_GRAY    1   /* Photometric interpretations */
  231. #define TIFF_PI_RGB     2
  232. #define TIFF_PI_PLTE    3
  233. #define TIFF_PC_CONTIG  1   /* Planar configurations */
  234. #define TIFF_RU_NONE    1   /* Resolution units */
  235. #define TIFF_RU_CM      3
  236. #define TIFF_ES_UNASSOC 2   /* Extra sample type */
  237.  
  238. /*
  239.  * Structure for holding miscellaneous image information. The
  240.  * conversion program will read an image into this structure, then
  241.  * pass it to the output function. In this implementation, the
  242.  * image data bytes are stored in a file, which is pointed to by
  243.  * this structure. This is so the code will work on small-memory
  244.  * architectures like MS-DOS. On Unix, Win32 (NT/Chicago), and other
  245.  * systems, it might make more sense to simply allocate one big
  246.  * chunk of memory for the image and replace the image_data_file
  247.  * string with an image_data_buffer pointer.
  248.  */
  249.  
  250. #define N_KEYWORDS 5
  251.  
  252. typedef struct _image_info {
  253.     U32 width, height;
  254.     U32 xoffset, yoffset;
  255.     U32 xres, yres;
  256.     double xscale, yscale;
  257.     double source_gamma;
  258.     U32 chromaticities[8];      /* Fixed point x 100000 */
  259.     int resolution_unit;        /* Units as in PNG */
  260.     int offset_unit, scale_unit;
  261.     int samples_per_pixel;
  262.     int bits_per_sample;
  263.     int significant_bits[4];
  264.     int background_color[4];
  265.     int is_color, has_alpha, has_trns;
  266.     int is_interlaced, is_palette;
  267.     int palette_size;
  268.     U8 palette[3 * 256];
  269.     U16 trans_values[3];
  270.     U8 palette_trans_bytes[256];
  271.     char *keywords[N_KEYWORDS];
  272.     char *pixel_data_file;      /* Where to find the pixels */
  273.     U32 png_data_size;
  274.     char *png_data_file;    /* Untranslatable PNG chunks */
  275. } IMG_INFO;
  276.  
  277. #define IMG_SIZE (sizeof (struct _image_info))
  278.  
  279. extern char *keyword_table[N_KEYWORDS];
  280. extern U16 ASCII_tags[N_KEYWORDS];
  281.  
  282. /*
  283.  * Local ASSERT macro.  Assumes the function Assert() is
  284.  * defined somewhere in the calling program (in this case,
  285.  * it's in ptot.c).
  286.  */
  287.  
  288. #ifndef NDEBUG
  289. #  define ASSERT(x) ((x)?(void)0:Assert(__FILE__,__LINE__))
  290. #  define TRACE_STR(x) (fprintf(stderr,"TR: %s\n",(x)),\
  291.                          fflush(stderr))
  292. #  define TRACE_INT(x) (fprintf(stderr,"TR: %ld\n",(long)(x)),\
  293.                          fflush(stderr))
  294. #else
  295. #  define ASSERT(x)
  296. #  define TRACE_STR(x)
  297. #  define TRACE_INT(x)
  298. #endif
  299.  
  300. /*
  301.  * Prototypes
  302.  */
  303.  
  304. U32 update_crc(U32, U8 *, U32);
  305.  
  306. int main(int argc, char *argv[]);
  307. void print_warning(int);
  308. void error_exit(int);
  309. void Assert(char *, int);
  310. int read_PNG(FILE *, IMG_INFO *);
  311. int get_chunk_header(void);
  312. U32 get_chunk_data(U32);
  313. int verify_chunk_crc(void);
  314.  
  315. int decode_IDAT(void);
  316. U8 fill_buf(void);
  317. void flush_window(U32);
  318. int decode_text(void);
  319. int copy_unknown_chunk_data(void);
  320. size_t new_line_size(IMG_INFO *, int, int);
  321.  
  322. int get_local_byte_order(void);
  323. int write_TIFF(FILE *, IMG_INFO *);
  324.  
  325. int create_tempfile(int); 
  326. int open_tempfile(int);
  327. void close_all_tempfiles(void);
  328. void remove_all_tempfiles(void);
  329.  
  330. /*
  331.  * Interface to Mark Adler's inflate.c
  332.  */
  333.  
  334. int inflate(void);
  335.  
  336. typedef unsigned char uch;
  337. typedef unsigned short ush;
  338. typedef unsigned long ulg;
  339. typedef void *voidp;
  340.  
  341. #define slide (ps.inflate_window)
  342. #define WSIZE ((size_t)(ps.inflate_window_size))
  343. #define NEXTBYTE ((--ps.bytes_in_buf>=0)?(*ps.bufp++):fill_buf())
  344. #define FLUSH(n) flush_window(n)
  345. #define memzero(a,s) memset((a),0,(s))
  346. #define qflag 1
  347.  
  348. /*
  349.  * A state structure is used to store all needed info about
  350.  * the reading process, so that we don't have to pass 4 or 5
  351.  * arguments to every function in ptot.c.  This is also used
  352.  * to share data with inflate.c.
  353.  */
  354.  
  355. #define IOBUF_SIZE 8192 /* Must be at least 768 for PLTE */
  356.  
  357. typedef struct _png_state {
  358.     FILE *inf, *tf[7];
  359.     char *tfnames[7];
  360.     IMG_INFO *image;
  361.     U8 *buf, *bufp;
  362.     U32 crc, bytes_remaining;
  363.     U32 inflated_chunk_size;
  364.     U32 current_chunk_name;
  365.     S32 bytes_in_buf;       /* Must be signed! */
  366.     U32 inflate_window_size;
  367.     U8 *inflate_window;
  368.     U16 inflate_flags;
  369.     U16 sum1, sum2;
  370.     U8 *last_line, *this_line;
  371.     size_t byte_offset;
  372.     size_t line_size, line_x;
  373.     int interlace_pass;
  374.     U32 current_row, current_col;
  375.     int cur_filter;
  376.     int got_first_chunk;
  377.     int got_first_idat;
  378. } PNG_STATE;
  379.